<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜索框和 添加商品按钮区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGosdsList">
            <el-button slot="append" icon="el-icon-search" @click.stop="getGosdsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="goGoodsAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品表格区域 -->
       <el-table
          :data="goodsList"
          style="width: 100%"
          border stripe>
          <el-table-column
            prop="goods_name"
            label="商品名称">
          </el-table-column>
          <el-table-column
            prop="goods_price"
            label="商品价格"
            width="100">
          </el-table-column>
          <el-table-column
            prop="goods_weight"
            label="商品重量"
            width="100">
          </el-table-column>
           <el-table-column
            prop="add_time"
            label="创建时间"
            width="150">
            <template v-slot="slotProps">
            {{ slotProps.row.add_time | formatDate}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template v-slot="slotProps">
              <el-button type="primary" size="mini" class="el-icon-edit">编辑</el-button>
              <el-button type="danger" size="mini" class="el-icon-delete" @click="removeGoods(slotProps.row.goods_id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      goodsList: [], // 商品列表数据
      queryInfo: { // 获取商品列表数据需要用到的参数对象
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0
    }
  },
  created () {
    this.getGosdsList()
  },
  methods: {
    async getGosdsList () { // 获取商品列表
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) return this.$msg.error('获取商品列表数据失败！')
      this.$msg.success('获取商品列表数据成功！')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (newVal) { // 分页功能函数，每页多少条数据
      this.queryInfo.pagesize = newVal
      this.getGosdsList()
    },
    handleCurrentChange (newVal) { // 分页功能函数 当前页
      this.queryInfo.pagenum = newVal
      this.getGosdsList()
    },
    async removeGoods (id) { // 点击删除按钮删除商品
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) return this.$msg.error('删除失败！')
      this.$msg.success('删除成功！')
      this.getGosdsList()
    },
    goGoodsAdd () { // 点击跳转添加商品页面
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>

</style>
